<?php
	require_once("../process/session.php");
	$my_session = new session();
	$db = $my_session->getDB_torneig();
?>
<html>
	<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Generador d'horaris</title>
        <link rel="stylesheet" type="text/css" href="../estils/style.css">
	</head>
	<body>
    	<div id="capçal" class="ombra">
    	
    		<h1 id="h1_head"> Generador d'horaris<h1>

		</div>
		
		<div id="main" class="ombra">
			<div id="menu">
				<ul>
					<li class="nivell1"><a class="nivell1" href="../forms/index.php">Inici</a></li>
					<li class="nivell1"><a class="nivell1" href="#">Participants</a>
						<ul class="pestanya"><span></span>
							<li><a href="../forms/f_inscrip.php">Inscriure's</a></li>
							<li><a href="#">Informació</a></li>
							<li><a href="#">Fotos</a></li>
						</ul>
					</li>
					<li class="nivell1"><a class="nivell1" href="#">Organització</a>
						<ul class="pestanya"><span></span>
							<li><a href="../forms/f_admin.php">Administrar torneig</a></li>
							<li><a href="#">Crear torneig</a></li>
						</ul>
					</li>
				</ul>
			</div>	

			 
			<h2> Configurar <?php echo $_SESSION['torneig'];?> </h2> <br>
			

				<h3> Mostrar informació</h3>
				<form name="info" action="../forms/f_admin_equips.php" method="POST" accept-charset="utf-8">
						 
						Obtenir informació de: 
						<select name = "info" onchange="this.form.submit()">
							<option value="null"> -- Seleccionar -- </option>
							<option value="parts"> Participants </option>
							<option value="equips"> Equips </option>
							<option value="equips_cats"> Categories </option>
						</select>
						<input type="submit" value="Oculta">
				</form>

			<?php
				if(isset($_POST['info'])){
					switch($_POST['info']){
						case "parts":
							echo "<h3> Informació participants </h3>"; 
							$query = "Select * From participants";
							$res = $db->consulta($query);
							echo "Total participants: ". sizeof($res)." <br>";
							?>
							<table border="1">
							<tr>
								<th> Dni </th>
								<th> Nom </th>
								<th> Telefon </th>
								<th> Correu </th>
								<th> Sexe </th>
								<th> Nivell </th>
							</tr>
							<?php
								for ($i=0; $i < sizeof($res); $i++) { 
							?>
							<tr>
								<?php
									for ($j=0; $j < 6; $j++) { 
								?>
									<td> <?php echo $res[$i][$j]; ?> </td>
								<?php
									}
								?>
							</tr>
							<?php
								}
							?>
							</table>
							<?php
							
						break;
						case "equips":
							echo "<h3> Informació equips </h3>"; 
							$taula = array();
							$query = "Select nom, pagament From equips Order By categoria";
							$eqs = $db->consulta($query);

							for ($i=0; $i < sizeof($eqs); $i++) { 
								$query = "Select dni, nom From participants, equip_participants Where dni_part = dni and nom_eq = '".$eqs[$i]['nom']."'";
								$res = $db->consulta($query);
								$taula[$i] = array();
								for ($j=0; $j < sizeof($res); $j++) { 
									$taula[$i][$j] = array();
									$taula[$i][$j][0] = $res[$j]['dni'];
									$taula[$i][$j][1] = $res[$j]['nom'];
								}
							}

							echo "Total equips: ". sizeof($taula)." <br>";

							for ($i=0; $i < sizeof($taula); $i++) {
								
									echo "<br>Equip ". $eqs[$i]['nom'];
									$q = "Select pagament From info";
									$pag = $db->consulta($q);
									if($pag[0]['pagament'] == 's'){
										if($eqs[$i]['pagament'] == 'n')	echo " (Falta pagar)";
									}
									echo "<br>";						
							?>
								<table border="1"> 
								<tr>
									<th> Dni </th>
									<th> Nom </th>
								</tr>
								
								<?php
									for ($j=0; $j < sizeof($taula[$i]); $j++) { 
								?>
										<tr>
											<td> <?php echo $taula[$i][$j][0]; ?> </td>
											<td> <?php echo $taula[$i][$j][1]; ?> </td>
										</tr>
								<?php
									}
								?>
							
							
								</table>
							<?php						
									
								}
							?>
							
							<?php
							
						break;
						case "equips_cats":
							echo "<h3> Informació Categories</h3>"; 
							
							$taula = array();
							$query = "Select * From categories";
							$cats = $db->consulta($query);

							for ($i=0; $i < sizeof($cats); $i++) { 
								$query = "Select nom, pagament From equips Where categoria = '".$cats[$i]['nom']."'";
								$res = $db->consulta($query);
								$taula[$i] = array();
								for ($j=0; $j < sizeof($res); $j++) { 
									$taula[$i][$j] = array();
									$taula[$i][$j][0] = $res[$j]['nom'];
									$taula[$i][$j][1] = $res[$j]['pagament'];
								}
							}

							echo "Total categories: ". sizeof($taula)." <br>";

							$q = "Select pagament From info";
							$res = $db->consulta($q);
							$pag = $res[0]['pagament'];

							for ($i=0; $i < sizeof($taula); $i++) {
								
									echo "<br>Categoria ". $cats[$i]['nom']. " (". sizeof($taula[$i]) ." equips)";
									?>
									<table border ="1">
										<tr>
											<th> Participants mínims </th>
											<th> Participants màxims </th>
											<th> Sexe </th>
											<th> Nivell </th>
										</tr>
										<tr>
											<td> <?php echo $cats[$i]['min_part']?> </td>
											<td> <?php echo $cats[$i]['max_part']?> </td>
											<td> <?php echo $cats[$i]['sexes']?> </td>
											<td> <?php echo $cats[$i]['nivell']?> </td>
										</tr>
									</table>
									<?php
								

					
							?>
								<table border="1"> 
								<tr>
									<th> Equip </th>
									<th> Ha pagat? </th>
								</tr>
								
								<?php

									for ($j=0; $j < sizeof($taula[$i]); $j++) { 
								?>
										<tr>
											<td> <?php echo $taula[$i][$j][0]; ?> </td>
											
											<td>
											<?php
												if($pag == 's'){
													if($taula[$i][$j][1] == "s") 	echo "Si";
													else 							echo "No";
													
												} else {
													echo " - ";
												}
											?>
											</td>
										</tr>
								<?php
									}
								?>
							
							
								</table>
							<?php						
									
								}
							?>
							
							<?php
							
						break;
					}
				}
			
			?>

			<div class="requadre">
				<h3> Eliminar equip </h3>
				<?php					
					// TODO Verificar que $res té dades correctes
					$query = "Select nom From equips";
					$res = $db->consulta($query);
				?>
				<form action="../process/p_admin_equips.php" name="categories" method="post">
				    <select name="eliminar_equip">
				<?php
				 	for ($i=0; $i < sizeof($res); $i++) { 
				?>
				    <option value="<?php echo $res[$i]['nom']; ?>"><?php echo $res[$i]['nom']; ?></option>
				<?php
				    }
				?>
				    </select>
				<?php
				    //mysql_close($db);
				?>
				<input type="hidden" name="function" value="eliminar_equip">
				<input type="submit" value="Eliminar">
				</form>
				
				
			</div>

				<div class="requadre">
				<h3> Marcar Pagats </h3>
				<form name="mostra_pagats" action="../forms/f_admin_equips.php" method="POST" accept-charset="utf-8">
					<input type="hidden" name="function" value="mostra">
					<input type="submit" value="Mostra">
				</form>
				<form name="oculta_pagats" action="../forms/f_admin_equips.php" method="POST" accept-charset="utf-8">
					<input type="hidden" name="function" value="oculta">
					<input type="submit" value="Oculta">
				</form>				
						<?php
						
							if(isset($_POST['function']) && $_POST['function'] == "mostra"){
						
							$query = "Select nom, pagament From equips Order By nom";
							$res = $db->consulta($query);
							
							?>
							<form name="marcar_pagats" action="../process/p_admin_equips.php" method="POST" accept-charset="utf-8">
							<?php
							
							for($i=0; $i < sizeof($res); $i++){
							?>
								<input type="checkbox" name="<?php echo $res[$i]['nom']; ?>" value="<?php echo $res[$i]['nom']; ?>" <?php if($res[$i]['pagament'] == 's') echo "CHECKED"?>> <?php echo $res[$i]['nom']; ?>  <br>
						<?php
							}
						?>
								<input type="hidden" name="function" value="marcats">
								<input type="submit" value="Guardar">
							</form>
					
				
				
						<?php
							}
						?>
			</div>
			
				<h3> Modificar categories equips </h3>
				<?php
					$query = "Select nom From categories";
					$res = $db->consulta($query);
				?>
				<form action="../process/p_admin_equips.php" name="pistes" method="post">

				<table border = "1">
				   <tr>
				   		<th> Categoria Origen</th>
				   		<th> Categoria Destí</th>
				   </tr> 
				   <tr>	
				   		<td>
						<?php
						 	for ($i=0; $i < sizeof($res); $i++) { 
						?>
						    <input type="radio" name="origen" value="<?php echo $res[$i]['nom']; ?>"><?php echo $res[$i]['nom']; ?></option> <br>
						<?php
						    }
						?>
						</td>
						<td>
						<?php
						 	for ($i=0; $i < sizeof($res); $i++) { 
						?>
						    <input type="radio" name="desti" value="<?php echo $res[$i]['nom']; ?>"><?php echo $res[$i]['nom']; ?></option> <br>
						<?php
						    }
						?>
						</td>
					</tr>
				</table>
				<?php
				    //mysql_close($db);
				?>
				<input type="hidden" name="function" value="canviar_cat">
				<input type="submit" value="Modificar">
				</form>
				

			
			
		</div>
	</body>
</html>